package com.pybox.api;

import com.pybox.dto.Result;
import com.pybox.dto.TCanbinetEventLogsDto;
import com.pybox.oauth.conf.UserDto;
import com.pybox.service.TCanbinetEventLogsService;
import com.pybox.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 描述：TCanbinetEventLogs Api
* @author wuyingkai
* @date 2018-09-15
*/

@Slf4j
@RequestMapping("/TCanbinetEventLogs")
@Api(tags = "TCanbinetEventLogs管理接口", description = "TCanbinetEventLogs管理接口")
@RestController
public class TCanbinetEventLogsApi {

    @Autowired
    private TCanbinetEventLogsService service;

    @ApiOperation(value = "创建TCanbinetEventLogs", notes = "创建TCanbinetEventLogs")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiIgnore
    public Result<Boolean> save(@RequestBody TCanbinetEventLogsDto dto) {
        service.create(dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "删除TCanbinetEventLogs", notes = "删除TCanbinetEventLogs")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "id", value = "TCanbinetEventLogs id", dataType = "String", paramType = "path")
    @ApiIgnore
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        service.delete(id);
        return Result.ok(true);
    }

    @ApiOperation(value = "更新TCanbinetEventLogs", notes = "更新TCanbinetEventLogs")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ApiImplicitParam(name = "id", value = "TCanbinetEventLogs id", dataType = "String", paramType = "path")
    @ApiIgnore
    public Result<Boolean> update(@PathVariable("id") Integer id, @RequestBody TCanbinetEventLogsDto dto) {
        service.update(id, dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "查询TCanbinetEventLogs", notes = "查询TCanbinetEventLogs")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "TCanbinetEventLogs id", dataType = "String", paramType = "path")
    @ApiIgnore
    public Result<TCanbinetEventLogsDto> detail(@PathVariable("id") Integer id) {

        return Result.ok(service.detail(id));
    }

    @ApiOperation(value = "分页查询TCanbinetEventLogs", notes = "level 1 普通 2警告；")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiImplicitParams({
           @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "endTime", value = "endTime", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "userName", value = "userName", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "cabinetId", value = "cabinetId", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "objName", value = "objName", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "event", value = "event", dataType = "string", paramType = "query"),
           @ApiImplicitParam(name = "level", value = "level", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "state", dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "pageNo", value = "pageNo", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query")
    })
    public Result<Page<TCanbinetEventLogsDto>> page(
                        @RequestParam(required = false) String startTime,
                        @RequestParam(required = false) String endTime,
                        @RequestParam(required = false) String customerId,
                        @RequestParam(required = false) String userName,
                        @RequestParam(required = false) String cabinetId,
                        @RequestParam(required = false) String objName,
                        @RequestParam(required = false) String event,
                        @RequestParam(required = false) String level,
                        @RequestParam(required = false) String state,
                    @RequestParam Integer pageNo,
                    @RequestParam Integer pageSize) {

        Map<String, String> params = new HashMap<>();
                params.put("startTime",startTime);
                params.put("endTime",endTime);
                params.put("userName",userName);
                params.put("cabinetId",cabinetId);
                params.put("obj_name",objName);
                params.put("customerId",customerId);
                params.put("event",event);
                params.put("level",level);
                params.put("state",state);

        return Result.ok(service.page(pageNo, pageSize, params));
    }

    @ApiOperation(value = "确认告警", notes = "确认告警")
    @RequestMapping(value = "/updateStateByIds", method = RequestMethod.PUT)
    public Result<Boolean> updateStateByIds(@RequestBody  List<Integer> ids){
        for(Integer id:ids){
            service.updateStateById(id);
        }
        return Result.ok(true);
    }

    @ApiOperation(value = "确认全部告警", notes = "确认全部告警")
    @RequestMapping(value = "/updateAllState", method = RequestMethod.PUT)
    public Result<Boolean> updateAllState(){
        service.updateAllState();
        return Result.ok(true);
    }

    @ApiOperation(value = "导出文件", notes = "导出文件")
    @RequestMapping(value = "exportStuInfoExcel",method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "startTime", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "endTime", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "userName", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "cabinetId", value = "cabinetId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "objName", value = "objName", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "event", value = "event", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "level", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "state", value = "state", dataType = "string", paramType = "query")
    })
    public void exportStuInfoExcel(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String customerId,
            @RequestParam(required = false) String userName,
            @RequestParam(required = false) String cabinetId,
            @RequestParam(required = false) String objName,
            @RequestParam(required = false) String event,
            @RequestParam(required = false) String level,
            @RequestParam(required = false) String state,
            HttpServletResponse response
    ) throws IOException {

        Map<String, String> params = new HashMap<>();
        params.put("startTime",startTime);
        params.put("endTime",endTime);
        params.put("cabinetId",cabinetId);
        params.put("userName",userName);
        params.put("obj_name",objName);
        params.put("customerId",customerId);
        params.put("level",level);
        params.put("event",event);
        params.put("state",state);
        service.downExcel(response,params);
    }

    @ApiOperation(value = "查询待确认警告的数量",notes = "默认是当前人员同客户的确认警告的数量")
    @RequestMapping(value = "/getUnCheckCount", method = RequestMethod.GET)
    public Result<Integer> getUnCheckCount(){
        UserDto user = UserUtils.getCurrentUser();
        Result<Integer> result ;
        if (user.getCustomerId() !=0 &&user.getIsAdmin() !=2){
           result =  service.getUnCheckCount(UserUtils.getCustomerId());
            return result;
        }
       // result =  service.getUnCheckCount();
        return null;
    }

}